package com.qiantu.phone.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLRoomDBManager;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.event.RefreshFloorRoomEvent;
import com.qiantu.phone.event.RoomIconSelectEvent;
import com.qiantu.phone.ui.fragment.RoomIconFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


public class RoomTypeActivity extends AppActivity {
    private android.widget.FrameLayout roomTypeFragmentContainer;
    RoomIconFragment roomIconFragment;
    private String roomType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_room_type;
    }

    @Override
    protected void initView() {
        EventBus.getDefault().register(this);
        roomType = getIntent().getStringExtra("roomType");
        roomTypeFragmentContainer = findViewById(R.id.room_type_fragment_container);
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        roomIconFragment = RoomIconFragment.newInstance(false, true,roomType);
        transaction.add(R.id.room_type_fragment_container, roomIconFragment);
        transaction.commit();
        getTitleBar().setRightTitle(R.string.save);

    }

    @Override
    protected void initData() {
    }

    @Override
    public void onRightClick(View view) {
        String roomSerialNo = getIntent().getStringExtra("roomSerialNo");
        String floorSerialNo = getIntent().getStringExtra("floorSerialNo");
        String roomName = getIntent().getStringExtra("roomName");
        LLHttpManager.editRoom((LifecycleOwner) getContext(), roomSerialNo, floorSerialNo, roomName, roomType, new HttpCallback<HttpData<Void>>(null){
            @Override
            public void onSucceed(HttpData<Void> result) {
                LLRoomDBManager.getInstance(getContext()).editRoomInfo(roomSerialNo, roomName, floorSerialNo, roomType);
                EventBus.getDefault().post(new RefreshFloorRoomEvent());
                Intent data = new Intent();
                data.putExtra("roomType", roomType);
                EventBus.getDefault().post(new RefreshFloorRoomEvent());
                setResult(RESULT_OK, data);
                finish();
                toast(R.string.reset_success);
            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);
                toast("修改失败");
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onRoomIconSelectEvent(RoomIconSelectEvent event) {
        roomType = event.iconType;
    }
}
